package com.shine.demo.leetCode.leetCode1029;

import com.shine.demo.leetCode.entity.ListNode;

/**
 * 反转链表
 *
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 */
public class ReverseList {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        System.out.println(reverseList(l1));
    }

    public static ListNode reverseList(ListNode head) {
        //上一个节点
        ListNode pre = null;
        //下一个节点
        ListNode next;
        while(head!=null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
